$(document).ready(function() {
    $("#d_login").validate({
        event: "blur",
        rules: {
            user_name:{
                required: true
            },
            password:{
                required: true,
                string:true,
                minlength: 6,
                maxlength:16
            }
        },
        messages : {
            user_name:{
                required: '用户名不能为空'
            },
            password: {
                required: "密码不能为空",
                minlength: jQuery.validator.format("密码不能小于 {0}位数")
            }
        },
        focusInvalid: false,
        onkeyup: false,
        showErrors: showErrors,
        submitHandler: function(form){
            var params="app_fmt=json&user_name="+base64encode(encodeURI($('#user_name').val(),'utf-8'))
                +"&password="+base64encode($('#password').val());
            Ajax.call('/index.php?app_act=center/doctors/do_login', params,
                function (s){
                    s = eval('(' + s + ')');
                    switch(s['code']){
                        case 1:
                            location.href = '/center/doctors/do_index';
                            break;
                        case 2:
                            //账号不存在
                            $("#checkuser_name").html(s.msg);
                            return false;
                            break;
                        default:
                        //xipin.alert(s['status']);
                    }
                }
                , 'POST', 'JSON');
        }
    });

    var phoneInfo = {
        phone:function(){
            return jQuery("#phone").val();
        }
    };
    var user_name = {
        user_name:function(){
            return jQuery("#user_name").val();
        }
    };
    var  phoneremoteInfo = GetRemoteInfo('/index.php?app_act=center/doctors/user_phone_check&app_page=null&app_fmt=json', phoneInfo);
    var  usernameremoteInfo= GetRemoteInfo('/index.php?app_act=center/doctors/user_name_check&app_page=null&app_fmt=json', user_name);

    $("#d_register").validate({
        event: "blur",
        rules: {
            user_name:{
                required:true,
                string:true,
                remote:usernameremoteInfo
            },
            phone:{
                required:true,
                isMobile:true,
                remote:phoneremoteInfo
            },
            t_check_code:{
                required:true
            },
            password:{
                required: true,
                string:true,
                minlength: 6,
                maxlength:16
            },
            confirm_password:{
                required: true,
                string:true,
                minlength: 6,
                maxlength:16,
                equalTo:"#password"
            },
            d_agreement_flg: {
                required: true,
                string:true
            }
        },
        messages: {
            user_name:{
                required:"昵称不能为空",
                isUserName:"请输入合法EMAIL"
            },
            phone: {
                required:"手机号不能为空",
                isMobile:"请正确填写您的手机号"
            } ,
            t_check_code:{
                required:"验证码不能为空"
            },
            password: {
                required: "密码不能为空",
                minlength: jQuery.validator.format("密码不能小于 {0}位数")
            },
            confirm_password:{
                required: "重复密码不能为空",
                minlength: jQuery.validator.format("密码不能小于 {0}位数"),
                equalTo	:"两次密码输入不一样"
            },
            dr_checknumber:{
                required: "验证码不能为空"
            },
            d_agreement_flg: {
                required: "您同意接受用户协议吗？"
            }
        },
        showErrors: showErrors,
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function(form){
            var $cr = $("#d_agreement_flg");
            if($cr.is(":checked")){
                //$('.reg_reg_btn').parent().html('注册申请中,请稍后...');
                var params="app_fmt=json"+
                    "&user_name="+base64encode(encodeURI($('#user_name').val(),'utf-8'))+
                    "&phone="+base64encode(encodeURI($('#phone').val(),'utf-8'))+
                    '&check_code='+base64encode(encodeURI($("#t_check_code").val()))+
                    "&password="+base64encode($('#password').val())+
                    "&repassword="+base64encode($('#confirm_password').val())+
                    "&checknumber="+$('#dr_checknumber').val();
//                alert(params);return false;
                Ajax.call('/index.php?app_act=center/doctors/do_register', params,
                    function (s){
                        s = eval('(' + s + ')');
                        if(s.code ==1) {
                            location.href="/center/doctors/do_index"
                        }

                        alert(s.code);
//                        if(s['statusCode']==1) {
//                            location.href="/user/index/active"
//                        }else{
//                            if(s['statusCode']==0) {
//                                baison.alert(s['status']);
//                                location.href="/mem_center/index/do_index";
//                            }else{
//                                baison.alert(s['status']);
//                                if(s['statusCode'] == -102){
//                                    $("#dregvCode").click();
//                                }
//                                $("#misun_btn_agree").attr('class','misun_btn_agree1');
//                            }
//                        }

                    }
                    , 'POST', 'JSON');
            }else{
                alert('请查看注册协议');
            }
        }
    });
    $("#count_down").bind('click', function () {
        var phone = $("#phone").val();
        var length = phone.length;
        var that = this;
        if($(that).attr('ts') == 0) {
            if(length == 11 && /^(((1[0-9]{1})|(1[0-9]{1})|(1[0-9]{1}))+\d{9})$/.test(phone)) {
                Ajax.call('/index.php?app_act=center/doctors/send_check_code&app_page=null&app_fmt=json', {phone:phone},function (data) {
                    data = eval('(' + data + ')');
                    if(data.code == 1) {
                        timelose(0, data.time);
                        $(that).attr('ts', 1);
                    } else {
                        alert(data.msg);
                    }
                }, 'POST', 'JSON');
            }
        }
    });
});
var maxtime = 0; //剩余时间的时间戳
var is_start = 0;
//timelose(0,10);
function timelose(start,end){
    maxtime = end;
    is_start = start;
    CountDown();
}
function CountDown(){
    if(maxtime > 0){
        $("#count_down").html(maxtime+'后获取短信校验码');
        --maxtime;
        setTimeout(CountDown,1000);
    }else if(maxtime == 0){
        $("#count_down").html('获取短信校验码');
        $("#count_down").attr('ts', 0);
    }
}